import java.awt.AWTException;
import java.awt.Color;
import java.awt.HeadlessException;

import java.awt.Graphics;
import javax.swing.*;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;



import java.util.*;


import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class TinCDCanvas extends JPanel implements MouseListener, MouseMotionListener{
	
	
	Point cursorLocation;
	int x = 0;
	int y = 0;
	int oldX,oldY;
	final double PI = 3.14159265358979323846;
	Line2D.Double NULL = new Line2D.Double(0,0,0,0);
	boolean drawSavedItem = false;
	int panelHeight = 0;
	int panelWidth = 0;
	int newInstance = 0;
	double slope, inverseSlope, ratioX, ratioY, ratio, tempNum, tempNum2;
	double theta, newX, newY;
	private MainFrame TinCDCopy;
	Line2D.Double alkane[] = new Line2D.Double[100];
	int alkaneCounter = 0;
	
	private Color backgroundColor = new Color(0.0f,0.0f,0.0f);
	
	TinCDCanvas(MainFrame TinCD)
	{
		TinCDCopy = TinCD;
		addMouseMotionListener(this);
		addMouseListener(this);
		
		
		//Carbon carbon = new Carbon();
		//Hydrogen hydrogen = new Hydrogen();
		//AtomicBond atomicbond = new AtomicBond(carbon,hydrogen);
	
	}
	
	

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		//setPanel dimensions
		setCanvasDim();	
		Graphics2D gg = (Graphics2D)g;
		g.setColor(backgroundColor);
		g.fillRect(0, 0, panelWidth, panelHeight);
		
		//Sets the skeletal form lines to black
		
		g.setColor(new Color(0,0,0));
		
		
		gg.setPaint(Color.WHITE);
		gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		gg.setStroke(new BasicStroke(1.4f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
		
		drawALine(gg);
		//gg.drawLine(50,50,50+30,50+30);
		//gg.drawString("Brian Loves Raina!", 200, 200);
		
		
	}
	
	void testPrinter()
	{
		 
		
	}
	void setCanvasDim()
	{
		panelHeight = TinCDCopy.getHeight();
		panelWidth = TinCDCopy.getWidth();
	}
	void drawALine(Graphics2D gg)
	{
		//if(drawCurrentItem == true)
		{
			gg.draw(new Line2D.Double(oldX,oldY,newX,newY));
			
		}
		/*else
			for (int i = 0; i <alkaneCounter; i++)
			{
				gg.draw(alkane[i]);
			}
			gg.draw(new Line2D.Double(oldX,oldY,newX,newY));
		*/
	
		repaint();
	}
	
		
	void clearJPanel()
	{
		
		repaint();
	}
	
	public void setBackgroundColor(Color color)
	{
		backgroundColor = color;
		repaint();
	}

	public void figureTheta(int x, int y)
	{
		
		float result;
		float newTheta;
		//int result;
		theta = Math.atan2(y, x);
		result = (int)Math.round(theta/(PI/6));
		//result = Math.round(theta/(PI/2));
		newTheta = (float) (result*PI/6);
		//newX = (42*(Math.cos(newTheta))+x);
		//newY = (42*(Math.sin(newTheta))+y);
		newX = ((Math.cos(newTheta))+x);
		newY = ((Math.sin(newTheta))+y);
		//System.out.println(newX);
		//System.out.println(newY);
		//addAlkane(oldX,oldY,newX,newY);
	
	}
	
	public void addAlkane(int oldX, int oldY, double newX, double newY)
	{
		alkane[alkaneCounter++] = new Line2D.Double(oldX,oldY,newX,newY);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
		oldX = (int)(e.getPoint().getX());
		oldY = (int)(e.getPoint().getY());
		newX = oldX-30;
		newY = oldY-30;
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
		/*if(e.getModifiers() == e.BUTTON1_MASK)
		{
			drawSavedItem = true;
			//oldX = (int)(e.getPoint().getX());
			//oldY = (int)(e.getPoint().getY());
			
			
			repaint();
		
			//mainHandle.addMessage("You selected: " + selected);	//delete this
		}*/
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
		/*if(e.getModifiers() == e.BUTTON1_MASK)
		{
			//drawSavedItem = false;
			
			repaint();
		
			//mainHandle.addMessage("You selected: " + selected);	//delete this
		}
		*/addAlkane(oldX,oldY,newX,newY);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
		
		x = (int)(e.getPoint().getX());
		y = (int)(e.getPoint().getY());
		figureTheta(x,y);
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	
		/* x = (int)(e.getPoint().getX());
		 y = (int)(e.getPoint().getY());
		
		
		slope = (double)x/y;
		inverseSlope = (double)y/x;

		repaint();
		*/
	}
	
	public void testScreen(File file, String extension) throws IOException, AWTException
	{
		
		try {

			BufferedImage image = new BufferedImage(panelWidth, panelHeight, BufferedImage.TYPE_INT_RGB);

			Graphics2D g = image.createGraphics();

			super.paint(g);

			ImageIO.write(image, extension , new File(file.getAbsolutePath()+"."+extension));
			
			}
			catch(Exception e) {}
		
		
			
		
	}



	
	
	
}
